import routes from 'server/route/index';
import utils from 'server/core/utils';
import eventHandle from 'server/handle/eventHandle';
import {
    totalConfig,
    propName
} from 'server/config/templateConfig';
import PetModel from 'server/model/petModel';
import packageHandle from 'server/handle/packageHandle';
import partnerModel from 'server/model/partnerModel';
import partnerHandle from 'server/handle/partnerHandle';

let buildingsConfig = totalConfig.building;
let mapsConfig = totalConfig.map;
let mazeConfig = totalConfig.maze;
let cardsConfig = totalConfig.card;
let itemsConfig = totalConfig.item;
let petsConfig = totalConfig.pet;
let partnersConfig = totalConfig.partner;
let townBuildingId = totalConfig.base.townBuildingId;
let townBuildingConfig = totalConfig.townBuilding[townBuildingId];
let equipmentsConfig = totalConfig.equipment;

//制作工具
routes.setLogin('toolMake', [
    ['itemId', 'number']
], (player, msg, cb) => {
    let itemId = msg.itemId;
    let toolTableConfig = totalConfig.toolTable[260000];
    let itemInfo = toolTableConfig.tools[itemId]
    if (!itemInfo) {
        cb(1);
        return;
    }
    let needItems = itemInfo.items;
    //材料是否足够
    let ret = packageHandle.isEnoughByObject(player.bag, needItems);
    if (ret === -1) {
        cb(2);
        return;
    }
    //扣除道具
    packageHandle.removeItems(player.bag, needItems);
    packageHandle.addItem(player.bag, itemId, 1);
    cb(0);
}, {
    1: '工具itemId不存在',
    2: '材料不足'
});


//设置采集采伐采矿钓鱼工具
routes.setLogin('toolSetUp', [
    ['toolType', 'number'],
    ['bagIndex', 'number']
], (player, msg, cb) => {
    let toolType = msg.toolType;
    let bagIndex = msg.bagIndex;
    let item = player.bag.items[bagIndex];
    if (!item) {
        cb(1);
        return;
    }
    let itemId = item.templateId;
    let itemsConfigInfo = itemsConfig[itemId];
    if (!itemsConfigInfo.toolType || itemsConfigInfo.toolType !== toolType) {
        cb(2);
        return;
    }
    packageHandle.removeItemByPosition(player.bag, item.pos);
    if (toolType === 1) {
        player.bag.collectionItem = item; //采集工具
    } else if (toolType === 2) {
        player.bag.cutItem = item; //采伐工具
    } else if (toolType === 3) {
        player.bag.miningItem = item; //采矿工具
    } else if (toolType === 4) {
        player.bag.fishingItem = item; //钓鱼工具
    }
    cb(0);
}, {
    1: '物品不存在',
    2: '材料不足'
});

//招募伙伴recruit
routes.setLogin('partnerRecruit', [
    ['partnerId', 'number']
], (player, msg, cb) => {
    let partnerId = msg.partnerId;
    let partnerConfig = partnersConfig[partnerId];
    if (!partnerConfig) {
        cb(1);
        return;
    }
    let partner = partnerHandle.createPartner(partnerId);
    player.basicInfo.partnerIndex += 1;
    partner.indexId = player.basicInfo.partnerIndex;
    partner.status = 1;
    player.partnerTeams.push(partner);
    cb(0, {
        log: ''
    })
});



//出战
routes.setLogin('partnerBattle', [
    ['indexId', 'number'],
    ['battleIndex', 'number']
], (player, msg, cb) => {
    let indexId = msg.indexId;
    let battleIndex = msg.battleIndex;
    if (battleIndex !== 0 && battleIndex !== 1 && battleIndex !== 2) {
        cb(1);
        return;
    }
    let partnerTeams = player.partnerTeams;
    let index = partnerTeams.findIndex(item => {
        if (item) {
            return item.indexId === indexId
        } else {
            return false
        }
    });
    if (index === -1) {
        cb(2);
        return
    }
    let partner = partnerTeams[index];

    let temp = player.battleTeams[battleIndex];
    player.battleTeams[battleIndex] = partner;
    if (temp) {
        partnerTeams[index] = temp;
        temp.status = 1;
    } else {
        partnerTeams.splice(index, 1);
    }
    partner.status = 2;
    cb(0, {
        log: ''
    })
});



//休息
routes.setLogin('partnerRest', [
    ['indexId', 'number']
], (player, msg, cb) => {
    let indexId = msg.indexId;
    let battleTeams = player.battleTeams;
    let index = battleTeams.findIndex(item => {
        if (item) {
            return item.indexId === indexId
        } else {
            return false
        }
    });
    if (index === -1) {
        cb(1);
    }
    let partner = player.battleTeams[index];
    player.partnerTeams.push(partner);
    battleTeams[index] = null;
    partner.status = 1;
    cb(0, {
        log: ''
    })
});

//切换地图
routes.setLogin('changeMap', [
    ['mapType', 'number']
], (player, msg, cb) => {
    player.basicInfo.mapType = msg.mapType;
    cb(0);
});

//背包丢弃东西
routes.setLogin('bagDiscard', [
    ['bagIndex', 'number']
], (player, msg, cb) => {
    let bagIndex = msg.bagIndex;
    packageHandle.removeItemByPosition(player.bag, bagIndex);
    cb(0, {
        log: ''
    })
});

//商店购买东西
routes.setLogin('shopBuy', [
    ['itemId', 'number']
], (player, msg, cb) => {
    let itemId = msg.itemId;
    let itemList = townBuildingConfig.shop.itemList;
    let gold = itemList[itemId];
    if (!gold) {
        cb(1);
        return
    }
    if (packageHandle.isHasEmpty(player.bag, 1) === -1) {
        cb(2);
        return;
    }
    if (player.basicInfo.gold < gold) {
        cb(3);
        return
    }
    player.basicInfo.gold -= gold;
    packageHandle.addItem(player.bag, itemId, 1);
    cb(0, {
        log: ''
    })
}, {
    1: '物品不存在',
    2: '背包空间不足',
    3: '金币不足'
});

//装备
routes.setLogin('equip', [
    ['bagIndex', 'number'],
    ['partnerId', 'number'],
    ['weaponType', 'number'],
], (player, msg, cb) => {
    let bagIndex = msg.bagIndex;
    let partnerId = msg.partnerId;
    let weaponType = msg.weaponType; //0无1主手武器2副手武器
    let equipment = player.bag.items[bagIndex]
    if (!equipment) {
        cb(1);
        return;
    }

    //判断是不是装备
    if (parseInt(equipment.templateId / 10000) !== 51) {
        cb(2);
        return;
    }
    let partner = player.battleTeams.find((item) => {
        return item.indexId === partnerId;
    });
    if (!partner) {
        partner = player.battleTeams.find((item) => {
            return item.indexId === partnerId;
        });
    }
    if (!partner) {
        cb(3);
        return
    }
    //类型1武器2头3胸4手5脚6腿7戒指8耳环
    //weaponPart 武器部位1主手2单手2副手
    //weaponType 武器类型1剑2斧3刀5法杖6权杖7弓8法书9盾10匕首

    let equipmentConfig = equipmentsConfig[equipment.templateId];
    let originEquipment = null;
    let equipmentType = equipmentConfig.type;
    if (equipmentType === 1) { //武器
        let weaponPart = equipmentConfig.weaponPart;
        if (weaponPart === 1 || (weaponType === 1 && weaponPart === 2)) { //装备主手
            let originMainWeapon = partner.equipments.mainWeapon;
            partner.equipments.mainWeapon = equipment;
            packageHandle.removeItemByPosition(player.bag, bagIndex);
            if (originMainWeapon) {
                packageHandle.addItemComponent(player.bag, originMainWeapon);
            }
        } else if ((weaponType === 2 && weaponPart === 2) || weaponPart === 3) { //装备副手
            let originMinorWeapon = partner.equipments.minorWeapon;
            partner.equipments.minorWeapon = equipment;
            packageHandle.removeItemByPosition(player.bag, bagIndex);
            if (originMinorWeapon) {
                packageHandle.addItemComponent(player.bag, originMinorWeapon);
            }
        } else {
            cb(5);
            return
        }
    } else if (equipmentType !== 1) {
        if (equipmentType === 2) {
            originEquipment = partner.equipments.head;
            partner.equipments.head = equipment;
        } else if (equipmentType === 3) {
            originEquipment = partner.equipments.body;
            partner.equipments.body = equipment;
        } else if (equipmentType === 4) {
            originEquipment = partner.equipments.hand;
            partner.equipments.hand = equipment;
        } else if (equipmentType === 5) {
            originEquipment = partner.equipments.foot;
            partner.equipments.foot = equipment;
        } else if (equipmentType === 6) {
            originEquipment = partner.equipments.ring;
            partner.equipments.ring = equipment;
        } else if (equipmentType === 7) {
            originEquipment = partner.equipments.ear;
            partner.equipments.ear = equipment;
        }
        //移除物品
        packageHandle.removeItemByPosition(player.bag, bagIndex);
        if (originEquipment) {
            //添加物品到背包
            packageHandle.addItemComponent(player.bag, originEquipment);
        }
    }
    cb(0)
});

//装备卸下
routes.setLogin('equipUnload', [
    ['partnerId', 'number'],
    ['weaponPart', 'string'],
], (player, msg, cb) => {
    let partnerId = msg.partnerId;
    let weaponPart = msg.weaponPart; //0无1主手武器2副手武器
    let partner = player.battleTeams.find((item) => {
        return item.indexId === partnerId;
    });
    if (!partner) {
        partner = player.battleTeams.find((item) => {
            return item.indexId === partnerId;
        });
    }
    if (!partner) {
        cb(1);
        return
    }
    let equipment = partner.equipments[weaponPart]
    if (!equipment) {
        cb(2);
        return;
    }
    
    if (packageHandle.isHasEmpty(player.bag, 1) === -1) {
        cb(3);
        return;
    }
    partner.equipments[weaponPart] = 0;
    packageHandle.addItemComponent(player.bag, equipment);

    cb(0)
});